﻿@page "/"
@using BlazorSample;

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

@inject Radzen.DialogService DialogService

<BlazorComponent.TitledBorder HorizontalTitle="用户资料"
                              VerticalTitle="重要"
                              BorderColor="#2563eb" Width="@(((600+30)+"px").ToString())" Height="@(((600+30)+"px").ToString())">
    <img src="@imageUrl" width="600" height="600" />
</BlazorComponent.TitledBorder>

<RadzenButton Text="@text"></RadzenButton>

<AntDesign.InputNumber Formatter="Format2" Parser="Parse2" TValue="double" @bind-Value="svalue" Step="0.000001" Min="0" Max="0.3" />

<RadzenRow Gap="2rem">
    <RadzenColumn SizeXS="1" Style="height: 200px;">
        <RadzenCard Variant="Variant.Filled" Style="height: 700px;overflow:hidden">
            @if (@Slots.Count(w => w.CarrierId == 1) > 0)
            {
                <RadzenRadioButtonList Change="@SlotChange" @bind-Value=value TValue="SlotDto" Orientation="Radzen.Orientation.Horizontal">
                    <Items>
                        @foreach (var item in @Slots.Where(w => w.CarrierId == 1))
                        {
                            <RadzenRadioButtonListItem Text="@item.LocationId" Style="@SlotColor(item)" Value="@item" />
                        }
                    </Items>
                </RadzenRadioButtonList>
            }
        </RadzenCard>
    </RadzenColumn>

    <RadzenColumn SizeXS="1">
        <RadzenCard Style="height: 700px;overflow:hidden">
            @if (@Slots.Count(w => w.CarrierId == 2) > 0)
            {
                <RadzenRadioButtonList Change="@(arg=>SlotChange(arg))" @bind-Value=value TValue="SlotDto" Orientation="Radzen.Orientation.Horizontal">
                    <Items>
                        @foreach (var item in @Slots.Where(w => w.CarrierId == 2))
                        {
                            <RadzenRadioButtonListItem Text="@item.LocationId" Disabled="@(item.State==2)" Style="@SlotColor(item)" Value="@item" />
                        }
                    </Items>
                </RadzenRadioButtonList>
            }
        </RadzenCard>
    </RadzenColumn>

    <RadzenColumn SizeXS="2">
        <RadzenCard Style="height: 1000px;overflow:hidden">
            <BlazorComponent.FoupListComponent CarrierName="Carrier1"
                                               SlotNumbers="@(Enumerable.Range(1, 25).ToList())"
                                               SelectedSlotId=@SelectedSlotId
                                               OnSlotSelected="HandleSlotSelected">
            </BlazorComponent.FoupListComponent>
        </RadzenCard>
    </RadzenColumn>

    <RadzenColumn SizeXS="2">
        <RadzenCard Style="height: 1000px;overflow:hidden">
            <BlazorComponent.FoupListComponent CarrierName="Carrier2"
                                               SlotNumbers="@(Enumerable.Range(1, 25).ToList())"
                                               SelectedSlotId=@SelectedSlotId
                                               OnSlotSelected="HandleSlotSelected">
            </BlazorComponent.FoupListComponent>
        </RadzenCard>
    </RadzenColumn>
</RadzenRow>

<RadzenRow Gap="2rem">
    <RadzenColumn>
        <BlazorComponent.CustomBlazorComponent Name="hhhh" Button1CallbackChanged="ButtonCallbackChanged">

        </BlazorComponent.CustomBlazorComponent>
    </RadzenColumn>

    <RadzenColumn>
        <RadzenCard Variant="Variant.Outlined">
            <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">Upload client-side without Url</RadzenText>
            <RadzenUpload @ref="_upload" DeleteText="" ChooseText="" Accept=".png, .jpg, .jpeg, .tif, .tiff" Multiple="false" Icon="upload" Change=@OnClientChange Style="width: 100%" />
        </RadzenCard>
    </RadzenColumn>
</RadzenRow>


@code {
    private string imageUrl = "https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png?0";

    private string SelectedSlotId;

    private void HandleSlotSelected(string selectedSlotId)
    {
        SelectedSlotId = selectedSlotId;
        StateHasChanged();
    }

    private double svalue;

    private string Format2(double value)
    {
        return DoubleMeterToMicron(value).ToString() + "μm";
    }

    private string Parse2(string value)
    {
        return DoubleMicronToMeter(Convert.ToDouble(value.Replace("%", ""))).ToString();
    }

    public double DoubleMeterToMicron(double currentValue)
    {
        return (currentValue * 1000000.0);
    }

    public static double DoubleMicronToMeter(double currentValue)
    {
        return (currentValue / 1000000.0);
    }

    RadzenUpload _upload = null!;
    void OnClientChange(UploadChangeEventArgs args)
    {
        foreach (var file in args.Files)
        {
            try
            {
                long maxFileSize = 10 * 1024 * 1024;
                // read file
                var stream = file.OpenReadStream(maxFileSize);
                stream.Close();
            }
            catch (Exception ex)
            {
            }
        }
    }

    public async Task ButtonCallbackChanged(CallbackDataModel data)
    {

    }

    string text = "Hi";
    SlotDto? value = null;
    private List<SlotDto> Slots = new List<SlotDto>();

    protected override async Task OnInitializedAsync()
    {
        base.OnInitializedAsync();
        Random random = new Random();
        for (int i = 1; i <= 25; i++)
        {
            Slots.Add(new SlotDto
            {
                CarrierId = 1,
                IsSelect = false,
                LocationId = "C1." + i,
                SlotId = i,
                State = random.Next(1, 2),
            });
        }
        // for (int i = 1; i <= 25; i++)
        // {
        //     Slots.Add(new SlotDto
        //         {
        //             CarrierId = 2,
        //             IsSelect = false,
        //             LocationId = "C2." + i,
        //             SlotId = i,
        //             State = random.Next(1, 2),
        //         });
        // }
    }


    public async Task SlotChange(SlotDto value)
    {
        bool? confirm = await DialogService.Confirm("Are you sure?", "MyTitle", new ConfirmOptions() { OkButtonText = "Yes", CancelButtonText = "No" });
    }

    public string SlotColor(SlotDto value)
    {
        if (value.State == 1)
            return "color:blue";
        return "color:red";
    }

    public class SlotDto
    {
        public int SlotId { get; set; }

        public int CarrierId { get; set; }

        public string LocationId { get; set; }

        public bool IsSelect { get; set; }

        public int State { get; set; }
    }
}
